<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>用户基本资料</title>
    <link rel="stylesheet" href="/layui/css/layui.css" media="all">
    <style>
        .layui-upload-img {
            width: 80px;
            height: 80px;
            margin-right: 1px;
            border-radius: 50%;
        }
    </style>
</head>
<body>
<div class="layui-container" style="width:600px;">
    <form class="layui-form layui-form-pane" action="" lay-filter="userInfo">
        <input name="id" hidden>
        <div class="layui-form-item">
            <label class="layui-form-label" style="margin-top: 21px;">头像</label>
            <div class="layui-input-block" style="overflow: hidden;">
                <div style="float: left;">
                    <button type="button" style="margin-top: 21px;" class="layui-btn" id="userPhoto">上传大头照</button>
                </div>
                <div style="float: left;">
                    <img class="layui-upload-img" id="userPhotoImg" style="margin-left: 20px;">
                    <p id="demoText" style="margin-left: 33px;"></p>
                </div>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">账号</label>
            <div class="layui-input-block">
                <input type="text" name="username" placeholder="请输入账号" style="background:#211f1f!important" class="layui-input layui-disabled" disabled="disabled">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">真实姓名</label>
            <div class="layui-input-block">
                <input type="text" name="realName" placeholder="请输入真实姓名" style="background:#211f1f!important" class="layui-input layui-disabled" disabled="disabled">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">所属部门</label>
            <div class="layui-input-block">
                <input type="text" name="deptName" style="background:#211f1f!important" class="layui-input layui-disabled" disabled="disabled">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">昵称</label>
            <div class="layui-input-block">
                <input type="text" name="nickName" placeholder="请输入昵称" autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">手机号</label>
            <div class="layui-input-block">
                <input type="text" name="phone" placeholder="请输入手机号" lay-verify="required|phone" autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">邮箱</label>
            <div class="layui-input-block">
                <input type="text" name="email" lay-verify="email" placeholder="请输入邮箱" autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">性别</label>
            <div class="layui-input-block">
                <input type="radio" name="sex" value="1" title="男">
                <input type="radio" name="sex" value="2" title="女">
            </div>
        </div>
        <div class="layui-form-item">
            <div class="layui-input-block">
                <button type="submit" class="layui-btn" lay-submit="" lay-filter="submit">保存</button>
            </div>
        </div>
    </form>
</div>
</body>
<script src="/layui/layui.all.js"></script>
<script src="/js/core.util.js"></script>
<script>
    let layer = layui.layer;
    let $ = jQuery = layui.jquery;
    let form = layui.form;
    let upload = layui.upload;
    let element = layui.element;
    $(function () {
        CoreUtil.sendAjax("/sys/user", null, function (res) {
            if (res.data != null) {
                let data = res.data;
                $('#userPhotoImg').attr('src', data.photo);
                form.val("userInfo", {
                    "username": data.username
                    , "id": data.id
                    , "deptName": data.deptName
                    , "phone": data.phone
                    , "realName": data.realName
                    , "nickName": data.nickName
                    , "email": data.email
                    , "deptName": data.deptName
                    , "sex": data.sex
                });
            }
        }, "GET", false);

        //监听提交
        form.on('submit(submit)', function (data) {
            console.log(data);
            CoreUtil.sendAjax("/sys/user/updateUserInfoSelf", JSON.stringify(data.field), function (res) {
                layer.msg(res.msg);
            }, "PUT", false);
            return false;
        });

        //普通图片上传
        let uploadInst = upload.render({
            elem: '#userPhoto'
            , url: '/sys/uploadUserPhoto' //改成您自己的上传接口
            , method: 'post' //可选项。HTTP类型，默认post
            , headers: {authorization: CoreUtil.getData("access_token")} //接口的请求头。如：headers: {token: 'sasasas'}。注：该参数为 layui 2.2.6 开始新增
            , accept: 'images' //指定允许上传时校验的文件类型，可选值有：images（图片）、file（所有文件）、video（视频）、audio（音频）
            , auto: true //是否选完文件后自动上传。如果设定 false，那么需要设置 bindAction 参数来指向一个其它按钮提交上传
            , size: 8000 //设置文件最大可允许上传的大小，单位 KB。不支持ie8/9（0表示不限制）
            , drag: false //是否接受拖拽的文件上传，设置 false 可禁用。不支持ie8/9
            , before: function (obj) {
                //预读本地文件示例，不支持ie8
                obj.preview(function (index, file, result) {
                    $('#userPhotoImg').attr('src', result); //图片链接（base64）
                });
            }
            , done: function (res) {
                let notice = '';
                let demoText = $('#demoText');
                //如果上传失败
                if (res.code != 0) {
                    notice = '<span style="color: red;">上传失败，原因：' + res.msg + '</span>';
                } else {
                    notice = '<span style="color: #4cae4c;">上传成功</span>';
                }
                //显示提示语
                demoText.html(notice);
            }
            , error: function () {
                //演示失败状态，并实现重传
                let demoText = $('#demoText');
                demoText.html('<span style="color: #FF5722;">上传失败</span> <a class="layui-btn layui-btn-xs demo-reload">重试</a>');
                demoText.find('.demo-reload').on('click', function () {
                    uploadInst.upload();
                });
            }
        });
    })
</script>
</html>